const config = require('../model/config');
const users = require('../model/users.js');
class Config {
   /* 修改配置 */
   async updataConfigs(data) {
      let { bkimg, name, sign, notice } = data
      let ress = await config.findAll({ where: { id: 1 } })
      if (ress.length === 0) {
         let res = await config.create({ Bolog_bkimg: bkimg, Bolog_name: name, Bolog_sign: sign, Bolog_notice: notice })
         if (res) {
            return {
               code: 1,
               msg: '创建成功'
            }
         }else{
            return{
               code:0,
               msg:"创建失败"
            }
         }
      } else {
         let res = await config.update({ Bolog_bkimg: bkimg, Bolog_name: name, Bolog_sign: sign, Bolog_notice: notice }, { where: { id: 1 } })
         if (res[0] === 1) {
            return {
               code: 1,
               msg: '更新成功'
            }
         } else {
            return {
               code: 0,
               msg: '更新失败'
            }
         }
      }
   }
   /* 获取配置 */
   async getconfigs(){
      let res = await config.findAll({ where: { id: 1 } })
      let reee = await users.findOne({
         attributes: ["Users_corver","Users_name"],
         where:{
            id:1,
            Users_role:1,
         }
      })
      console.log(reee,"------------------------");
      return {
         code:1,
         data:res[0].dataValues,
         user:reee.dataValues
      }
   }
   /* 添加访问量 */
   async addview(){
      let res = await config.increment("Bolog_view",{ where: { id: 1 }})
      if(res){
         return {
            code:1,
            msg:"添加成功"
         }
      }else{
         return {
            code:0,
            msg:"添加失败"
         }
      }
   }
}

module.exports = new Config();
